package reflect;

import java.lang.reflect.Field;

public class ReflectDemo8 {
    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();

        Field field = cls.getDeclaredField("name");
        field.setAccessible(true);
        field.set(obj,"张三");
        String name = (String)field.get(obj);
        System.out.println(name);
        field.setAccessible(false);


        Field field2 = cls.getDeclaredField("age");
        field2.setAccessible(true);
        field2.set(obj,18);
        int age = (int) field2.getInt(obj);
        System.out.println(age);
        field2.setAccessible(false);







    }
}
